package com.ilink.dataanalysis.entity.VO;

import com.alibaba.fastjson.JSONObject;
import lombok.AllArgsConstructor;
import lombok.Data;

/**
 * @author dzy
 * 用于向百度前端返回指定格式的JSON字符串
 */
@Data
@AllArgsConstructor
public class CommonResponseVO {
    private int status = 0;
    private boolean hitSugarSelf = true;
    private String msg = "";
    private Object data;

    public CommonResponseVO(Object data){
        this.data = data;
    }

    @Override
    public String toString() {
        return JSONObject.toJSONString(this);
    }

    public static String error(Exception e){
        return JSONObject.toJSONString(new CommonResponseVO(1, false, "内部服务器错误：" + e.getMessage(), null));
    }
}

/*private static final ObjectMapper objectMapper = new ObjectMapper();

    public static String createResponseBody(Object data) {
        Map<String, Object> responseBody = new HashMap<>();
        responseBody.put("status", 0);
        responseBody.put("hitSugarSelf", true);
        responseBody.put("msg", "");
        responseBody.put("data", data);
        try {
            return objectMapper.writeValueAsString(responseBody);
        } catch (Exception e) {  //处理序列化异常，返回一个通用的错误响应
            return "{\"status\": 1, \"msg\": \"服务器内部错误\", \"data\": null}";
        }
    }*/